package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.OrderDetailMapper;
import com.zhongyu.mapper.PKYKMapper;
import com.zhongyu.mapper.StoreDetailMapper;
import com.zhongyu.mapper.StoreOperMapper;
import com.zhongyu.pojo.Panyingpankui;
import com.zhongyu.pojo.StoreDetail;
import com.zhongyu.pojo.StoreOper;
import com.zhongyu.pojo.User;
import com.zhongyu.service.StoreDetailService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.StoreOperVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
@Service("storeDetailService")
public class StoreDetailServiceImpl extends ServiceImpl<StoreDetailMapper, StoreDetail> implements StoreDetailService {
 @Resource
    private StoreDetailMapper storeDetailMapper;
 @Resource
 private PKYKMapper pkykMapper;
    @Resource
    private StoreOper storeOper;
    @Resource
    private StoreOperMapper storeOperMapper;
    @Override
    public PageResult searchAll(Integer pageNo, Integer pageSize, StoreOperVo storeOperVo) {
        LambdaQueryWrapper<StoreDetail> queryWrapper= Wrappers.<StoreDetail>lambdaQuery();
        queryWrapper.eq(storeOperVo.getStoreId()!=null,StoreDetail::getStoreId,storeOperVo.getStoreId())
                .eq(storeOperVo.getGoodId()!=null,StoreDetail::getGoodId,storeOperVo.getGoodId());
        Page page=new Page(pageNo,pageSize);
        List<StoreDetail> list=storeDetailMapper.findAll(page,queryWrapper);
//       System.out.println("==2>"+list.size());
        /* return PageResult.getInstanceByPage(page);*/
        return new PageResult(list,page.getTotal());

    }
    @Override
    public List<StoreDetail> searchGoods() {

        return storeDetailMapper.searchgoods();
    }

    @Override
    public List<StoreDetail> serachStoreId(Integer goodId) {
        return storeDetailMapper.searchStoreId(goodId);
    }

    @Override
    public void updatePyPk(StoreDetail storeDetail, HttpSession session) {
        User loginUser= (User) session.getAttribute("loginUser");
        pkykMapper.updateState(Panyingpankui.STATE_CHECK,loginUser.getId(), LocalDateTime.now(),storeDetail.getId());
        if(storeDetail.getType()==2){
        storeDetailMapper.updatejian(storeDetail.getStoreId(),storeDetail.getGoodId(),storeDetail.getNumber());
        }else if(storeDetail.getType()==1){
        storeDetailMapper.update(storeDetail.getStoreId(),storeDetail.getGoodId(),storeDetail.getNumber());
        }
        //添加库存变更操作记录
        storeOper.setOperateId(loginUser.getId());
        storeOper.setGoodId(storeDetail.getGoodId());
        storeOper.setNumber(storeDetail.getNumber());
        storeOper.setOperateTime(LocalDateTime.now());
        storeOper.setStoreId(storeDetail.getStoreId());
        if(storeDetail.getType()==2){
            storeOper.setType(StoreOper.TYPE_KUI);
        }else if(storeDetail.getType()==1){
            storeOper.setType(StoreOper.TYPE_YING);
        }

        storeOperMapper.insert(storeOper);
    }
}
